<?php
class ControllerSaleKeyword extends Controller {
	private $error = array();

  	public function index() {
		$this->load->language('sale/keyword');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/keyword');
    	$this->getList();
  	}
	
  	public function insert() {
		$this->load->language('sale/keyword');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/keyword');
		if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
      	  	$this->model_sale_keyword->addKeyword($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('sale/keyword', 'token=' . $this->session->data['token'], 'SSL'));
		}
    	$this->getForm_text();
    	$this->getForm_insert();
  	}
	
  	public function update() {
		$this->load->language('sale/keyword');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/keyword');
		if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			$this->model_sale_keyword->editKeyword($this->request->get['id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('sale/keyword', 'token=' . $this->session->data['token'], 'SSL'));
		}
    	$this->getForm_text();
    	$this->getForm_update();
  	}

  	private function getList() {
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('sale/keyword', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
   		$this->data['insert'] = $this->url->link('sale/keyword/insert', 'token=' . $this->session->data['token'], 'SSL');
   		
  		$url = '';
		if (isset($this->request->get['filter_category_id'])) {
			$filter_category_id = $this->request->get['filter_category_id'];
			$url .= '&filter_category_id=' . $filter_category_id;
		} else {
			$filter_category_id = null;
		}
		if (isset($this->request->get['filter_language_id'])) {
			$filter_language_id = $this->request->get['filter_language_id'];
			$url .= '&filter_language_id=' . $filter_language_id;
		} else {
			$filter_language_id = null;
		}
		if (isset($this->request->get['filter_keyword'])) {
			$filter_keyword = $this->request->get['filter_keyword'];
			$url .= '&filter_keyword=' . urlencode(html_entity_decode($filter_keyword, ENT_QUOTES, 'UTF-8'));
		} else {
			$filter_keyword = null;
		}
		if (isset($this->request->get['filter_show_home'])) {
			$filter_show_home = $this->request->get['filter_show_home'];
			$url .= '&filter_show_home=' . $filter_show_home;
		} else {
			$filter_show_home = null;
		}
		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
			$url .= '&filter_status=' . $filter_status;
		} else {
			$filter_status = null;
		}
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'k.click_num';
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'DESC';
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$data = array(
			'filter_category_id'  => $filter_category_id,
			'filter_language_id'  => $filter_language_id,
			'filter_keyword'      => $filter_keyword,
			'filter_show_home'    => $filter_show_home,
			'filter_status'       => $filter_status,
			'sort'      => $sort,
			'order'     => $order,
			'start'     => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'     => $this->config->get('config_admin_limit')
		);
		
		$total = $this->model_sale_keyword->getTotalKeywords($data);
		$results = $this->model_sale_keyword->getKeywords($data);
		$this->data['keywords'] = array();
    	foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('sale/keyword/update', 'token=' . $this->session->data['token'] . '&id=' . $result['id'], 'SSL')
			);
			$this->data['keywords'][] = array(
				'category_name'         => $result['category_name'],
				'category_display_name' => $result['category_display_name'],
				'language_name'         => $result['language_name'],
				'language_image'        => $result['language_image'],
				'keyword'      => $result['keyword'],
				'click_num'    => $result['click_num'],
				'show_home'    => $result['show_home'],
				'status'       => $result['status'],
				'action'       => $action
			);
		}

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_missing'] = $this->language->get('text_missing');
		$this->data['text_all'] = $this->language->get('text_all');
		$this->data['text_status_yes'] = $this->language->get('text_status_yes');
		$this->data['text_status_no'] = $this->language->get('text_status_no');
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		
		
    	$this->data['column_category_name'] = $this->language->get('column_category_name');
    	$this->data['column_category_display_name'] = $this->language->get('column_category_display_name');
	    $this->data['column_language'] = $this->language->get('column_language');
    	$this->data['column_keyword'] = $this->language->get('column_keyword');
    	$this->data['column_click_num'] = $this->language->get('column_click_num');
		$this->data['column_show_home'] = $this->language->get('column_show_home');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_action'] = $this->language->get('column_action');

		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_filter'] = $this->language->get('button_filter');

		$this->data['token'] = $this->session->data['token'];
		
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
  		if ($order == 'ASC') {
			$url_sort = $url . '&order=DESC';
		} else {
			$url_sort = $url . '&order=ASC';
		}
		$this->data['sort_keyword'] = $this->url->link('sale/keyword', 'token=' . $this->session->data['token'] . '&sort=k.keyword' . $url_sort, 'SSL');
		$this->data['sort_click_num'] = $this->url->link('sale/keyword', 'token=' . $this->session->data['token'] . '&sort=k.click_num' . $url_sort, 'SSL');
		$this->data['sort_show_home'] = $this->url->link('sale/keyword', 'token=' . $this->session->data['token'] . '&sort=k.show_home' . $url_sort, 'SSL');
		$this->data['sort_status'] = $this->url->link('sale/keyword', 'token=' . $this->session->data['token'] . '&sort=k.status' . $url_sort, 'SSL');
		
  		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $sort;
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $order;
		}
		$pagination = new Pagination();
		$pagination->total = $total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('sale/keyword', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$this->data['pagination'] = $pagination->render();

		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
				
		$this->load->model('product/category');
		$this->data['categories'] = $this->model_product_category->getAllCategoriesForSelect(0, '', '', -1, 0);
		
		$this->data['filter_category_id'] = $filter_category_id == null ? 0 : $filter_category_id;
		$this->data['filter_language_id'] = $filter_language_id == null ? 0 : $filter_language_id;
		$this->data['filter_keyword'] = $filter_keyword;
		$this->data['filter_show_home'] = $filter_show_home == null ? -1 : $filter_show_home;
		$this->data['filter_status'] = $filter_status == null ? -1 : $filter_status;
		
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		 
		$this->template = 'sale/keyword_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		
		$this->response->setOutput($this->render());
  	}

  	public function getForm_text() {
  		$this->data['breadcrumbs'] = array();
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('sale/keyword', 'token=' . $this->session->data['token'], 'SSL'),				
			'separator' => ' :: '
		);
		
		$this->data['heading_title'] = $this->language->get('heading_title');
		 
		$this->data['text_no_results'] = $this->language->get('text_no_results');  
		$this->data['text_default'] = $this->language->get('text_default');

		$this->data['text_all'] = $this->language->get('text_all');
		$this->data['text_status_yes'] = $this->language->get('text_status_yes');
		$this->data['text_status_no'] = $this->language->get('text_status_no');
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		
		$this->data['entry_category'] = $this->language->get('entry_category');
		$this->data['entry_language'] = $this->language->get('entry_language');
    	$this->data['entry_keyword'] = $this->language->get('entry_keyword');
		$this->data['entry_click_num'] = $this->language->get('entry_click_num');
		$this->data['entry_show_home'] = $this->language->get('entry_show_home');
		$this->data['entry_status'] = $this->language->get('entry_status');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
 		if (isset($this->error['keyword'])) {
			$this->data['error_keyword'] = $this->error['keyword'];
		} else {
			$this->data['error_keyword'] = '';
		}

		$this->data['token'] = $this->session->data['token'];
		
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
				
		$this->load->model('product/category');
		$this->data['categories'] = $this->model_product_category->getAllCategoriesForSelect(0, '', '', -1, 0);
		
  	}
	
	public function getForm_insert() {
  		$this->data['action'] = $this->url->link('sale/keyword/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('sale/keyword', 'token=' . $this->session->data['token'], 'SSL');

    	$this->data['category_id'] = 0;
    	$this->data['language_id'] = 0;
		$this->data['keyword'] = '';
		$this->data['click_num'] = 0;
		$this->data['show_home'] = 0;
		$this->data['status'] = 0;
		
		$this->template = 'sale/keyword_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		
		$this->response->setOutput($this->render());
  	}

	public function getForm_update() {
  		$this->data['action'] = $this->url->link('sale/keyword/update', 'token=' . $this->session->data['token'] . '&id=' . $this->request->get['id'], 'SSL');
		$this->data['cancel'] = $this->url->link('sale/keyword', 'token=' . $this->session->data['token'], 'SSL');
		
    	$keyword_info = $this->model_sale_keyword->getKeyword($this->request->get['id']);
		$this->data['category_id'] = $keyword_info['category_id'];
		$this->data['language_id'] = $keyword_info['language_id'];
		$this->data['keyword'] = $keyword_info['keyword'];
		$this->data['click_num'] = $keyword_info['click_num'];
		$this->data['show_home'] = $keyword_info['show_home'];
		$this->data['status'] = $keyword_info['status'];
		
		$this->template = 'sale/keyword_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		
		$this->response->setOutput($this->render());
  	}
}
?>